// pages/my/shipments/shipments.js
const App = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    selectilall:false,
    selectilAllNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  //获取可发货列表
  getData:function () {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: App.globalData.url+'/niu/apiv3/niu_getuserkuv3_full.php',
      method: 'GET',
      data: {
        page:0,
        markid:wx.getStorageSync('markId'),
        sort:11,
        mainstate:1,
        type:0
      },
      header: {
        'PUA': 'wxsapp'
      },
      success: (res) => {
        console.log(res.data)
        wx.hideLoading()
        this.setData({
          list:res.data.data
        })
      }
    })
  },
  //单选调用
  select:function(e){
    let selectValue = e.currentTarget.dataset.name;
    let index = e.currentTarget.dataset.index;
    let list = this.data.list;
    let selectilall = this.data.selectilall;
    let selectilAllNum = this.data.selectilAllNum;
    let newli = 'list[' + index + '].checked';
    if (!this.data.list[index].checked) {
      selectilAllNum ++
    }else{
      selectilAllNum --;
      selectilall = false
    }
    if (list.length == selectilAllNum) {
      selectilall = true
    }
    this.setData({
      [newli]: !this.data.list[index].checked,
      selectilAllNum:selectilAllNum,
      selectilall:selectilall
    })
    // console.log(selectilAllNum);
    // console.log(list)
  },
  //全选调用
  selsctAll:function(e){
    let list = this.data.list;
    let selectilall = this.data.selectilall;
    let selectilAllNum = this.data.selectilAllNum;
    if (selectilall==false){
      for (let i = 0; i < list.length; i++) {
        let newli = 'list[' + i + '].checked';
        //carts[i].selected = this.data.selectedAllStatus;
        //this.data.select.push(this.data.list[i].id);
        this.setData({
          //[newli]: !this.data.list[i].checked
          [newli]: true,
          selectilall: true,
          selectilAllNum:list.length
        })
      }
    }else{
      for (let i = 0; i < list.length; i++) {
        let newli = 'list[' + i + '].checked';
        //carts[i].selected = this.data.selectedAllStatus;
        //this.data.select.push(this.data.list[i].id);
        this.setData({
          //[newli]: !this.data.list[i].checked
          [newli]: false,
          selectilall: false,
          selectilAllNum:0
        })
      }
    }
    
    console.log(list);
  },
  //立即发货
  shipment:function (){
    let checkedId = [];
    let list = this.data.list;
    for (let i = 0; i < list.length; i++) {
      if (list[i].checked) {
        checkedId.push(list[i].id);
      }
    }
    if (checkedId.length == 0) {
      wx.showToast({
        title: '请选择发货商品',
        icon:'none',
        duration: 2000
      })
      return
    }else{
      let kutoysids = checkedId.join(';');
      console.log(kutoysids);
      utils.requestUrl({//将用户信息传给后台数据库
        // method:'post',
        url: "/niu/api/fahuo_checktoys.php",
        params: {
          markid: wx.getStorageSync('markId'),
          kutoysids:kutoysids
        }
      }).then((res) => {
        var data = res.data;
        console.log(data);
        if (data.resultcode == '0') {
          wx.navigateTo({
            url: '/pages/my/myOrder/myOrder?kutoysids='+kutoysids,
          })
        }else{
          utils.toast(data.msg)
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})